package pl.devfest.task;

import java.sql.SQLException;
import java.util.List;

import pl.devfest.GeneralActivity;
import pl.devfest.R;
import pl.devfest.model.Task;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class TasksActivity extends GeneralActivity {
	private List<Task> allTasks;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task);

		try {
			allTasks = getHelper().getTasksDao().queryForAll();
			TaskAdapter taskAdapter = new TaskAdapter(this, allTasks);

			ListView lv = (ListView) findViewById(R.id.list_tasks);
			lv.setAdapter(taskAdapter);

			lv.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int pos, long arg3) {
					Task task = allTasks.get(pos);

					Intent i = new Intent(TasksActivity.this,
							TaskViewActivity.class);
					i.putExtra(TaskViewActivity.EXTRA_TASK_ID, task.getId());
					startActivity(i);

				}
			});
		} catch (SQLException e) {
		}

	}
}
